/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.basic.park.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.hopes.bizz.basic.park.entity.BaseCalendarEntity;
import com.hopes.bizz.basic.park.service.BaseCalendarService;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 日历
 *
 * @author pig
 * @date 2024-01-22 14:31:55
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/calendar" )
@Tag(description = "calendar" , name = "日历管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BaseCalendarController {

    private final BaseCalendarService baseCalendarService;

    /**
     * 查询
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/list/{date}" )
    @PreAuthorize("@pms.hasPermission('park_calendar_view')" )
    public R getbaseCalendarList(@DateTimeFormat(pattern = "yyyy-MM") @PathVariable("date") Date date) {
        List<BaseCalendarEntity> calendarEntityList = baseCalendarService.lambdaQuery().ge(BaseCalendarEntity::getDateDate, date)
                .lt(BaseCalendarEntity::getDateDate, DateUtil.offsetMonth(date,1)).list();
        return R.ok(calendarEntityList);
    }


    /**
     * 通过id查询日历
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('park_calendar_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(baseCalendarService.getById(id));
    }

    /**
     * 新增日历
     * @param baseCalendar 日历
     * @return R
     */
    @Operation(summary = "新增日历" , description = "新增日历" )
    @SysLog("新增日历" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('park_calendar_add')" )
    public R save(@RequestBody BaseCalendarEntity baseCalendar) {
        return R.ok(baseCalendarService.save(baseCalendar));
    }

    /**
     * 修改日历
     * @param baseCalendar 日历
     * @return R
     */
    @Operation(summary = "修改日历" , description = "修改日历" )
    @SysLog("修改日历" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('park_calendar_edit')" )
    public R updateById(@RequestBody BaseCalendarEntity baseCalendar) {
        return R.ok(baseCalendarService.updateById(baseCalendar));
    }

    /**
     * 通过id删除日历
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除日历" , description = "通过id删除日历" )
    @SysLog("通过id删除日历" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('park_calendar_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(baseCalendarService.removeBatchByIds(CollUtil.toList(ids)));
    }

}
